package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.auth.vo.SocialUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理社交登陆的Controller
 * @ClassName Oauth2Controller
 * @Description TODO
 * @Author Administrator
 * @Date 2022/7/3 15:15
 * @Version 1.0
 **/
@Slf4j
@Controller
public class Oauth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/oauth2/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code换token
        Map<String, String> map = new HashMap<>();
        map.put("grant_type","authorization_code");
        map.put("code",code);
        map.put("client_id","c26195356938d0d30cd0a21dd03a8f289af4f5bb1493a7e958b45b07ffcd9aab");
        map.put("client_secret","25e7065a23a04d42cc8fdd38c1573b4b2cfd1ba2bdffa2288cd97964d54918d5");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2/gitee/success");

        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(),map,new HashMap<>());
        // 处理token
        if (response.getStatusLine().getStatusCode()==200){
            //获取到了token
            String json = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSON.parseObject(json);
            SocialUser user = JSON.parseObject(json, SocialUser.class);
            user.setUid(jsonObject.getString("id"));
            //当前社交用户
            //1) 当前用户如果是第一次登陆，自动注册(自定生成会员信息账号),关联本系统账号信息
            //登陆或注册这个社交用户
            R r = memberFeignService.oauthLogin(user);
            if (r.getCode() == 0){
                MemberRespVo data = r.getData("data", new TypeReference<MemberRespVo>() {});
                //第一次使用session，命令浏览器保存卡号
                //子域之间 *.gulimall.com
                //发卡的时候(指定域名为父域名)即使是子域发的，父域也能用
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                log.info("登陆成功{}",data.toString());
                //登陆成功跳会首页
                return "redirect:http://gulimall.com";
            }else {
                return "redirect:http://gulimall.com/login.html";
            }
        }else {
            return "redirect:http://gulimall.com/login.html";
        }
    }

}
